﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;

namespace _4591BRD
{
    class MakePersonalData
    {
        private int charID = 0;
        private DateTime curDate;

        public MakePersonalData(DateTime date, int charid)
        {
            curDate = date;
            charID = charid;
        }

        public void Run(object state)
        {
            Common.AddThreadCount();
            //Log.WriteLine(1, "Make CharID = " + charID.ToString() + ", Date = " + curDate.ToShortDateString());

            try
            {
                CharDataObj objDataDaily = CurrentDateManager.Instance.GetCharData(charID, 0);
                CharDataObj objDataMonthly = CurrentDateManager.Instance.GetCharData(charID, 1);
                CharDataObj objDataAll = CurrentDateManager.Instance.GetCharData(charID, 2);

                string monStr = curDate.ToString("yyyy-MM", System.Globalization.DateTimeFormatInfo.InvariantInfo);
                string dateStr = curDate.ToString("yyyy-MM-dd", System.Globalization.DateTimeFormatInfo.InvariantInfo);

                string pathDaily = Common.WebDataPath + "Daily\\" + dateStr + "\\CharData\\ID_" + ((int)(charID / 10000) * 10000).ToString() + "\\";
                string pathMonthly = Common.WebDataPath + "Monthly\\" + monStr + "\\CharData\\ID_" + ((int)(charID / 10000) * 10000).ToString() + "\\";
                string pathAll = Common.WebDataPath + "Total\\CharData\\ID_" + ((int)(charID / 10000) * 10000).ToString() + "\\";

                if (!Directory.Exists(pathAll))
                    Directory.CreateDirectory(pathAll);
                if (!Directory.Exists(pathMonthly))
                    Directory.CreateDirectory(pathMonthly);
                if (!Directory.Exists(pathDaily))
                    Directory.CreateDirectory(pathDaily);

                string dataFileDaily = pathDaily + "CharData_" + charID.ToString() + ".txt";
                string dataFileMonthly = pathMonthly + "CharData_" + charID.ToString() + ".txt";
                string dataFileAll = pathAll + "CharData_" + charID.ToString() + ".txt";

                StreamWriter swDataDaily = new StreamWriter(dataFileDaily, false, Encoding.GetEncoding("GB2312"));
                StreamWriter swDataMonthly = new StreamWriter(dataFileMonthly, false, Encoding.GetEncoding("GB2312"));
                StreamWriter swDataAll = new StreamWriter(dataFileAll, false, Encoding.GetEncoding("GB2312"));

                //生成时间
                string dateCreateStr = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", System.Globalization.DateTimeFormatInfo.InvariantInfo);
                swDataDaily.WriteLine("CreateTime " + dateCreateStr);
                swDataMonthly.WriteLine("CreateTime " + dateCreateStr);
                swDataAll.WriteLine("CreateTime " + dateCreateStr);

                //排名数据
                foreach (KeyValuePair<string, int> obj in objDataDaily.RankData)
                {
                    swDataDaily.WriteLine("CharData " + obj.Key + " " + obj.Value);
                }
                foreach (KeyValuePair<string, int> obj in objDataMonthly.RankData)
                {
                    swDataMonthly.WriteLine("CharData " + obj.Key + " " + obj.Value);
                }
                foreach (KeyValuePair<string, int> obj in objDataAll.RankData)
                {
                    swDataAll.WriteLine("CharData " + obj.Key + " " + obj.Value);
                }

                //基本战斗数据
                foreach (KeyValuePair<string, int> obj in objDataDaily.ObjData)
                {
                    swDataDaily.WriteLine("CharData " + obj.Key + " " + obj.Value);
                }
                foreach (KeyValuePair<string, int> obj in objDataMonthly.ObjData)
                {
                    swDataMonthly.WriteLine("CharData " + obj.Key + " " + obj.Value);
                }
                foreach (KeyValuePair<string, int> obj in objDataAll.ObjData)
                {
                    swDataAll.WriteLine("CharData " + obj.Key + " " + obj.Value);
                }

                //使用武器击杀次数
                foreach (KeyValuePair<int, int> obj in objDataAll.WeaponKill)
                {
                    swDataAll.WriteLine("CharData " + "WeaponKill_" + obj.Key.ToString() + " " + obj.Value);
                }

                //击杀目标次数
                DataTable tbData = new DataTable();
                DataTable tmpTB;

                tbData.Columns.Add("TargetID", typeof(int));
                tbData.Columns.Add("Count", typeof(int));

                foreach (KeyValuePair<int, int> obj in objDataDaily.KillObj)
                {
                    tbData.Rows.Add(obj.Key, obj.Value);
                }
                tbData.DefaultView.Sort = "Count desc";
                tmpTB = tbData.DefaultView.ToTable().Copy();
                for (int i = 0; i < tmpTB.Rows.Count; i++)
                {
                    DataRow row = tmpTB.Rows[i];
                    int objID = int.Parse(row["TargetID"].ToString());
                    swDataDaily.WriteLine("KillObj " + objID.ToString() + " " + NameCacheManager.Instance.GetCharNameByID(objID) + " " + row["Count"].ToString());
                }
                tbData.Clear();

                foreach (KeyValuePair<int, int> obj in objDataMonthly.KillObj)
                {
                    tbData.Rows.Add(obj.Key, obj.Value);
                }
                tbData.DefaultView.Sort = "Count desc";
                tmpTB = tbData.DefaultView.ToTable().Copy();
                for (int i = 0; i < tmpTB.Rows.Count; i++)
                {
                    DataRow row = tmpTB.Rows[i];
                    int objID = int.Parse(row["TargetID"].ToString());
                    swDataMonthly.WriteLine("KillObj " + objID.ToString() + " " + NameCacheManager.Instance.GetCharNameByID(objID) + " " + row["Count"].ToString());
                }
                tbData.Clear();

                foreach (KeyValuePair<int, int> obj in objDataAll.KillObj)
                {
                    tbData.Rows.Add(obj.Key, obj.Value);
                }
                tbData.DefaultView.Sort = "Count desc";
                tmpTB = tbData.DefaultView.ToTable().Copy();
                for (int i = 0; i < tmpTB.Rows.Count; i++)
                {
                    DataRow row = tmpTB.Rows[i];
                    int objID = int.Parse(row["TargetID"].ToString());
                    swDataAll.WriteLine("KillObj " + objID.ToString() + " " + NameCacheManager.Instance.GetCharNameByID(objID) + " " + row["Count"].ToString());
                }
                tbData.Clear();

                swDataDaily.Close();
                swDataMonthly.Close();
                swDataAll.Close();
            }
            catch (IOException se)
            {
                Log.WriteError(se.Message);
            }
            catch (Exception e)
            {
                Log.WriteError(e.Message);
            }

            //线程工作结束
            //Log.WriteLine(1, "Finish Make CharID = " + charID.ToString() + ", Date = " + curDate.ToShortDateString());
            Common.RemoveThreadCount();
        }
    }
}
